---
--类名: TypeWordsView.lua
--创建日期: 2015-10-26
--创建人: 陈小虎
--功能描述: 屏幕打字视图
--修改内容:
--修改日期:   
--修改人:     
--修改备注: 
--
local Vector = require("app.utils.Vector")

local TypeWordsView = class("TypeWordsView",function()
    return display.newLayer("TypeWordsView")
end)

function TypeWordsView:ctor(words,fontSize,delayTime,typeTime,overDelay,lineLen,color,typeOverCall)

	self.mWords = words
	self.mFontSize = fontSize
	self.mDelayTime = delayTime
	self.mTypeTime = typeTime
	self.mOverDelayTime = overDelay
	self.mLineLen = lineLen
	self.mColor = color
	self.mTypeOverCall = typeOverCall

	self.mWordsTab = string.utable(self.mWords)

	self.mPointer = 1
	self.mWaitTime = 0
	self.mOverWaitTime = 0

	self.ttfConfig = {}
    self.ttfConfig.fontFilePath = Res.font
    self.ttfConfig.fontSize = self.mFontSize
	-- local tempLabel = cc.Label:createWithTTF(self.ttfConfig,self.mWords, cc.VERTICAL_TEXT_ALIGNMENT_CENTER, self.mLineLen)
	local tempLabel = cc.Label:createWithSystemFont(self.mWords, "arial", self.mFontSize,cc.size(self.mLineLen,0),cc.VERTICAL_TEXT_ALIGNMENT_CENTER,cc.VERTICAL_TEXT_ALIGNMENT_CENTER)

	self.mPosX = (display.width - self.mLineLen) / 2 - self.mFontSize / 2
	self.mPosY = (display.height - tempLabel:getBoundingBox().height) / 2 + tempLabel:getBoundingBox().height

	self.pos = cc.p((display.width - self.mLineLen) / 2 - self.mFontSize / 2,self.mPosY)
	self.mDrawWidth = 0
	self.mLine = 0

	self.mLineWords = Vector.new()

	local function excute()
		self:scheduleUpdate( handler(self, self.updateFrame))
    	
	end

	local action = cc.Sequence:create(cc.DelayTime:create(self.mDelayTime),cc.CallFunc:create(excute))
	self:runAction(action)
end

function TypeWordsView:overCall()
	if self.mTypeOverCall then
		self.mTypeOverCall()
	end
end

function TypeWordsView:updateFrame(dt)
	if not self.mWordsTab then
		self:overCall()
		return
	end
	local maxWords = #self.mWordsTab
	if self.mPointer > maxWords then
		-- self.mOverWaitTime = self.mOverWaitTime + dt
		-- if self.mOverWaitTime >= self.mOverDelayTime then
		-- 	self:unscheduleUpdate()
		-- 	print("TypeWordsView:updateFrame ------ over 1")

		self:overCall()
		return
	end

	local intervalTime = self.mTypeTime / maxWords

	self.mWaitTime = self.mWaitTime + dt
	if self.mWaitTime >= intervalTime then
		self.mWaitTime = 0
		local typeWord = self.mWordsTab[self.mPointer]

    	local word = cc.Label:createWithTTF(self.ttfConfig,typeWord, cc.TEXT_ALIGNMENT_LEFT, self.mFontSize)
    	word:setTextColor( cc.c4b(self.mColor.r, self.mColor.g, self.mColor.b, self.mColor.a))
    	word:setAnchorPoint(cc.p(0,0))
    	self:addChild(word)

    	local wordConfig = {}
    	wordConfig.word = word
    	wordConfig.width = word:getBoundingBox().width

    	-- word:setPosition(self.pos)

    	local wordHeight = word:getBoundingBox().height
    	local wordWidth = word:getBoundingBox().width


    	
    	self.mDrawWidth = self.mDrawWidth + word:getBoundingBox().width
    	if self.mDrawWidth >= self.mLineLen then
    		self.mLineWords:clear()
    		self.mLineWords:add(wordConfig)
    		self.mDrawWidth = 0
    		self.mLine = self.mLine + 1
    	else
    		self.mLineWords:add(wordConfig)
    	end


    	local wordsWidth = 0
    	for i = 1,self.mLineWords:size() do
    		local wordConfig = self.mLineWords:get(i)
    		wordsWidth = wordsWidth + wordConfig.width
    	end

    	local x = (display.width - wordsWidth) / 2
    	local posY = self.mPosY - self.mLine * wordHeight

    	for i = 1,self.mLineWords:size() do
    		local wordConfig = self.mLineWords:get(i)
    		wordConfig.word:setPosition(cc.p(x,posY))

    		x = x + wordConfig.width
    	end


		self.mPointer = self.mPointer + 1
	end



end

function TypeWordsView:getStringLength(str)
    local _, count = string.gsub(str, "[^\128-\193]", "")
    return count
end

return TypeWordsView